<?php

/**
 * @author zhuludong
 * @copyright 2018.8.10
 */

namespace Firegame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

class CrontabController extends ApiController {
    

    // 用户在线1小时,获取一张优惠券(数据已取消)
    public function getPrize()
    {
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        $start_time = strtotime(date('Y-m-d'));
        $time = time();

        $map['update_time'] =  array('egt',$start_time);
        $map['update_time'] =  array('elt',$end_time);

        $info = array();
        $userList = M('users')->field('id,update_time')->where("update_time>=$start_time AND update_time<=$end_time")->select();
        if(!empty($userList)) {
            foreach($userList as $k=>$v) {
                if(($time - $v['update_time']) >= 3600) {
                   $info[] = $v['id'];
                }
            }
            if(!empty($info)) {
                $userStr = implode(',',$info);
                $where['id'] = array('in',$userStr);
                $data['lottery'] = array('exp','lottery + 1');
                $data['update_time'] = $time;
                $ret = M('users')->where($where)->save($data);
                if(empty($ret)) {
                    Log::write('操作失败用户在线1小时获取一张优惠券','ERROR');
                }
                Log::write('操作成功用户在线1小时获取一张优惠券','SUCCESS');
            }   
        }
        
    }

    // 定时插入数据到仇人榜
    public function handleFoeman() {

    }

    // 每日23:59:59 定时重置每日抽奖次数
    public function handlePrize() {
        $map['sign_num'] = array('egt',7);
        $ret = M('users')->field('id')->where($map)->select();
        if(!empty($ret)) {
            $userArr = array_column($ret,'id');
            $userStr = implode(',', $userArr);
            $params['id'] = array('in',$userStr);
            $ret = M('users')->where($map)->save(array('sign_num'=>0));
            if(empty($ret)) {
                Log::write('重置用户签到大于7的次数','ERROR');
            }
            $contion['uid'] = array('in',$userStr);
            $res = M('users')->where($contion)->save(array('status'=>0));
            if(empty($res)) {
                Log::write('更新每日登陆状态失败','ERROR');
            }
        }
    }

}